package com.jsonengine.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class JEexUtils {

    public static final String YAHOO_PROTOCOL_JA = "http://";

    public static final String YAHOO_HOST_JA = "jlp.yahooapis.jp";

    public static final String YAHOO_MASERVICE_JA = "/MAService/V1/parse";

    public static final String YAHOO_APPID_JA_ =
        "appid="
            + "AXH7j4Sxg64itdF.3PrCnuTxm_JUXzBkMQL5hYGkmhiecO0yUKrWUpQ.GHXN.jDK3TN_.g--";

    public static final String YAHOO_RESULT_JA = "results=" + "uniq";

    public static final String YAHOO_RESPONSE_JA = "response="
        + "surface,reading";

    public static final String YAHOO_FILTER_JA = "filter=" + "1|3|5|6|7|8|9|10";

    public static final String YAHOO_MA_RESPONSE_JA = "ma_response=" + "";

    public static final String YAHOO_MA_FILTER_JA = "ma_filter=" + "";

    public static final String YAHOO_UNIQ_RESPONSE_JA = "uniq_response=" + "";

    public static final String YAHOO_UNIQ_FILTER_JA = "uniq_filter=" + "";

    public static final String YAHOO_UNIQ_BY_BASEFORM_JA = "uniq_by_baseform="
        + "";

    public static final String YAHOO_SENTENCE_JA = "sentence=";

    /**
     * Calls the Yahoo API URL and returns the response text.
     * 
     * @param text
     * @return String of the response
     */
    public String callYahooAPI(String text) {
        final StringBuilder sb = new StringBuilder();
        try {
            URL url =
                new URL(YAHOO_PROTOCOL_JA + YAHOO_HOST_JA + YAHOO_MASERVICE_JA);
            HttpURLConnection connection =
                (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");

            OutputStreamWriter writer =
                new OutputStreamWriter(connection.getOutputStream());
            String param =  YAHOO_FILTER_JA
                    + "&" + YAHOO_RESPONSE_JA
                    + "&" + YAHOO_APPID_JA_
                    + "&" + YAHOO_SENTENCE_JA
                    + URLEncoder.encode(text, "UTF-8");
            writer.write(param);
            writer.close();

            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                BufferedReader reader =
                    new BufferedReader(new InputStreamReader(
                        connection.getInputStream(),
                        "UTF-8"));
                String line;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                reader.close();
            } else {
                // output error log
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

}
